#!/usr/bin/env python

import sys
#from optparse import OptionParser

def parse_file(file):
    samples = dict()
    infile = open(file,"r")
    last = 0
    while infile:
        line = infile.readline()
        s = line.split()
        n = len(s)
        if n == 0:
            break
        if s[0] == '#':
            continue
        n_threads = int(s[0])
        block_size = int(s[1])
        tm = float(s[4])
        tme = float(s[5])
        if last != n_threads:
            #print "starting %s"%n_threads
            last = n_threads
            samples[n_threads] = list()
        samples[n_threads].append([block_size,tm, tme])
        #print s;
    return samples

def main():
    #infname = "queue_mark.dat"
    #outbase = "queue_mark_%s.dat"
    #for x in sys.argv:
    #    print "Argument: ", x
    #print "num args ", len(sys.argv)
    if len(sys.argv) != 3:
        print "syntax: %s in_file out_pattern"%sys.argv[0]
        exit
    infname = sys.argv[1]
    outpatt = sys.argv[2]
    samples = parse_file(infname)
    #print samples
    for nt, s in samples.items():
        fname = outpatt%(nt)
        f = open(fname, "w")
        for l in s:
            f.write("%d %f %f\n"%(l[0], l[1], l[2]))

if __name__ == '__main__':
    #import profile
    #profile.run('main()')
    main()

